#include "player.h"

void initializePlayer(void)
{
    changeAnimation(&player, "fatmar1.png");
    //player.sprite = loadImage("fatmar1.png");
    player.x = 20;
    player.y = 300;
    
    player.direction = RIGHT;
    player.etat = IDLE;
   
 //   player.frameNumber = 0;
   // player.frameTimer = TIME_BETWEEN_2_FRAMES;

    player.w = PLAYER_WIDTH;
    player.h = PLAYER_HEIGTH;
    jeu.nombreFireballs = 0;

    player.timerMort = 0;
    player.onGround = 0;

    jeu.nombreMonstres = 0;

}

void updatePlayer(void)
{
    int y;
    if (player.timerMort == 0)
    {
        player.dirX = 0;

        player.dirY += GRAVITY_SPEED;

        if (player.dirY >= MAX_FALL_SPEED)
        {
            player.dirY = MAX_FALL_SPEED;
        }
        

        if (input.left == 1)
        {
            player.dirX -= PLAYER_SPEED;

            if(player.direction == RIGHT)
            {
                player.direction = LEFT;
                player.sprite = loadImage("fatmal.png");
                 if(player.etat != WALK_LEFT && player.onGround == 1)
        {
            player.etat = WALK_LEFT;
            changeAnimation(&player, "fatmal.png");
        }
            }
        }

        else if (input.right == 1)
        {
            player.dirX += PLAYER_SPEED;

            player.direction = RIGHT;

        if(player.etat != WALK_RIGHT && player.onGround == 1)
        {
            player.etat = WALK_RIGHT;
            changeAnimation(&player, "fatmar1.png");
        }
        }
        else if(input.right == 0 && input.left == 0 && player.onGround == 1)
    {
        //On teste si le joueur n'était pas déjà inactif, pour ne pas recharger l'animation
        //à chaque tour de boucle

        if(player.etat != IDLE)
        {
            player.etat = IDLE;
            //On change l'animation selon la direction
            if(player.direction == LEFT)
            {
                player.sprite = loadImage("IdleLeft.png");
                player.frameNumber = 0;
            }
            else
            {
                player.sprite = loadImage("IdleRight.png");
                player.frameNumber = 0;
            }

        }

    }
        
         if (input.jump == 1)
    {
           if(player.onGround == 1)
           {
            player.dirY = -JUMP_HEIGHT;
            
            player.onGround = 0;
            player.jump = 1;

             playSoundFx(JUMp);
           }
        
        else if (player.jump == 1)
        {
            player.dirY = -JUMP_HEIGHT;
            player.jump = 0;
           if (player.dirY < 0)
           {
            player.dirY=0;

           }


        
       playSoundFx(JUMp);
        }
        
        input.jump = 0;
    }
     if(input.enter == 1)
    {
        //On met le jeu en pause
        jeu.onMenu = 1;
        jeu.menuType = PAUSE;
        input.enter = 0;
    }
    if(input.attack == 1)
    {
        createFireBall();
        input.attack = 0;
    }
 
   
    if (player.onGround == 1)
        player.jump = 1;
    if(player.y> map.maxY)
    {
        player.y = -  map.maxY - player.h ;
    }
    
    
        mapCollision(&player);
        centerScrollingOnPlayer();
    }

    if (player.timerMort > 0)
    {
        player.timerMort--;
        if (player.timerMort == 0)
        {
            /* Si on est mort */
            jeu.vies--;
            if(jeu.vies < 0)
               // jeu.vies = 0;
            {
                //On retourne au menu start
                jeu.onMenu = 1;
                jeu.menuType = START;
            }
            initializePlayer();
            
           
        }
    }
}
void centerScrollingOnPlayer()
 {
    map.startX = player.x - (SCREEN_WIDTH / 2);

    if (map.startX < 0)
    {
        map.startX = 0;
    }
    else if (map.startX + SCREEN_WIDTH >= map.maxX)
    {
        map.startX = map.maxX - SCREEN_WIDTH;
    }

 }